#!/usr/bin/env python
#timedef.py

"""G-Start time definition

Author: Miroslav Zadravec
Date: 2010-02-06

Describes a timed program start.

"""

class TimeList:
    _queue = []
    
    def count(self):
        return len(self._queue)
        
    def push(self, tdef):
        """Add new time definition to the queue."""
        self._queue.append(tdef)
        
    def tick(self):
        """Reduces the time left to execute a process.
        If time left is zero, process is removed from the queue.
        
        Returns: TimeDef object whose time is reduced
        """
        
        if len(self._queue) == 0:
            print "Empty list"
            return None
        
        self._queue[0].tick()
        if self._queue[0].time_left() <= 0:
            tdef = self._queue.pop(0)
            return tdef
        else:
            tdef = self._queue[0]
            return tdef

    def __iter__(self):
        self._itercurent = 0
        return self                 # simplest iterator creation
    
    def next(self):
        curiter = self._itercurent
        if len(self._queue) > curiter:
            self._itercurent += 1
            return self._queue[curiter]                        
        else:
            raise StopIteration


class TimeDef:
    _name = ""
    _time_left = 0
    _path = ""
    _active = False
    
    def __init__(self, name, offsettime, path):
        self._name = name
        self._time_left = int(offsettime)
        self._path = path
        self._active = True
        
    def time_left(self):
        return self._time_left
    
    def done(self):
        self._active = False
        self._time_left = 0
        
    def name(self):
        return self._name
    
    def path(self):
        return self._path
        
    def tick(self):
        if self._time_left > 0:
            self._time_left -= 1
        #print self._path + " time left: " + str(self._time_left)
            
    def __repr__(self):
          return str(self._time_left) + ": " + self._path
          
    def active(self):
	return self._active


